package com.logicwin.aibala.ui.screens.screenmain;

import com.logicwin.aibala.R;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.logicwin.aibala.detailPackActivity.DetailDesc;
import com.logicwin.aibala.model.androidSWInfo.APKListItem;
import com.logicwin.aibala.services.file.AsyncImage;
import com.logicwin.aibala.services.file.ImageManager;
import com.logicwin.aibala.utils.MyLog;
import com.logicwin.aibala.utils.StringUtils;

public class ApkPackageAdapter extends BaseAdapter implements AdapterView.OnItemClickListener{
	
	private final int mHighestLevel = 5;
	private final int mLowestLevel  = 0;
	
	private String mType = "";
	private String mSort = "";
	
	protected Activity mParentActivity;
	protected List<APKListItem> mApkList;
	
	private final String tag = "ApkPackageAdapter";
	
	
	public ApkPackageAdapter(Activity activity, List<APKListItem> apkList, String type, String sort){
		mParentActivity = activity;
		mApkList = apkList;
		mType = type;
		mSort = sort;
	}
	
	
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ScreenHome.getViewCount++;
		MyLog.info(tag, "getView ScreenHome.getViewCount="+ScreenHome.getViewCount);
		MyLog.info(tag,"getView :: mType="+mType+" ScreenHome.mCurrentType="+ScreenHome.mCurrentType+" mSort="+mSort+" ScreenHome.mCurrentSort="+ScreenHome.mCurrentSort);
		
		View view = convertView;
		APKListItem item;

		if (view == null) {		
			view = mParentActivity.getLayoutInflater().inflate(R.layout.package_item, null);
		}
		
		
		if ( (!(mType.equals(ScreenHome.mCurrentType)&&mSort.equals(ScreenHome.mCurrentSort)))
				&& !mType.equals("search")){
			return view;
		}
		
		if ((item = this.mApkList.get(position)) == null) {
			return view;
		}
		inflateItemView(view, item);
		Button btnDownload = (Button) view.findViewById(R.id.package_download);
		btnDownload.setOnClickListener(new ListViewItemButtonClickListener(item,"download"));
		
		Button btnCancel = (Button) view.findViewById(R.id.package_cancel);
		btnCancel.setVisibility(Button.INVISIBLE);
		btnCancel.setOnClickListener(new ListViewItemButtonClickListener(item,"cancel"));
		return view;
	}	
	
	
	public void inflateItemView(View view, APKListItem item){
		MyLog.info("ApkPackageAdpter", "name = "+item.getAPKName());
		
		
		final ImageView packimage = (ImageView) view.findViewById(R.id.package_image);

			Bitmap cacheImage = AsyncImage.asyncLoadIconImage(item.getIconURL(), new AsyncImage.ImageCallback() {
				public void imageLoaded(Bitmap image) {
					packimage.setImageBitmap(image);
				}
			});
			if (cacheImage != null) {
				packimage.setImageBitmap(cacheImage);
			}
			else {
				packimage.setImageResource(R.drawable.image);
			}
		
		
		((TextView) view.findViewById(R.id.package_name)).setText(item.getAPKName());	
		((TextView) view.findViewById(R.id.package_size)).setText(item.getAPKSize());
		((TextView) view.findViewById(R.id.package_times)).setText(item.getDownloadCount());
		
		inflateLevel(view, item.getRefStarCount());		
	}
	private void inflateLevel(View view, int level){
		RatingBar bar = (RatingBar) view.findViewById(R.id.stars);
		if(level<mLowestLevel) level = mLowestLevel;
		if(level>mHighestLevel) level = mHighestLevel;
		bar.setRating(level);
	}

	public List<APKListItem> getCurrApkList(){
		return mApkList;
	}
	public void setCurrApkList(List<APKListItem> apklist, String type, String sort){
		mApkList = apklist;
		mType =type;
		mSort = sort;
	}
	public Activity getParentActivity(){
		return mParentActivity;
	}
	@Override
	public int getCount() {
		if (mApkList==null)
			return 0;
		return mApkList.size();
	}
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mApkList.get(position);
		//return null;
	}
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {

		Log.i("ApkPackageAdapter", mApkList.get(position).getAPKName());
		Intent intent = new Intent(mParentActivity, DetailDesc.class);

		intent.putExtra("apkId", mApkList.get(position).getID());
		mParentActivity.startActivity(intent);
		
	}	
}
